#!/usr/bin/bash
systemctl restart libvirtd
function check()
{
if [ $? == 0 ]
then
echo -e "\033[32m${1}=======${vm_name}-${action_args}=======================>success\033[0m"
else
echo -e "\033[31m${1}=======${vm_name}-${action_args}=======================>fail\033[0m"
fi
}
k8s_nodes="k8s-master01 k8s-master02 k8s-master03 k8s-node01 k8s-node02"
ceph_nodes="ceph01 ceph02 ceph03 ceph04 ceph05 ceph06 ceph07"
exam_nodes="exam-k8s01 exam-k8s02 exam-k8s03"

function create_k8s_snap()
{
action_args="create snapshot"
for vm_name in $k8s_nodes;
do
virsh shutdown $vm_name >/dev/null 2>&1 
sleep 4;
virsh snapshot-create-as ${vm_name} k8s-finish >/dev/null 2>&1 
check;
done
}

function create_ceph_snap()
{
action_args="create snapshot"
for vm_name in $ceph_nodes;
do
virsh shutdown $vm_name >/dev/null 2>&1 
sleep 4;
virsh snapshot-create-as ${vm_name} ceph-finish >/dev/null 2>&1 
check;
done
}

function reset_exam_k8s()
{
action_args="recover snapshot"
for vm_name in $exam_nodes;
do
virsh destroy $vm_name >/dev/null 2>&1 
sleep 1;
virsh snapshot-revert $vm_name k8s-finish --running >/dev/null 2>&1 
check;
done
}

function reset_k8s()
{
action_args="recover snapshot"
for vm_name in $k8s_nodes;
do
virsh destroy $vm_name >/dev/null 2>&1 
sleep 1;
virsh snapshot-revert $vm_name k8s-finish --running >/dev/null 2>&1 
check;
done
}

function reset_ceph()
{
action_args="recover snapshot"
for vm_name in $ceph_nodes;
do
virsh destroy $vm_name >/dev/null 2>&1 
sleep 1;
virsh snapshot-revert $vm_name ceph-finish --running >/dev/null 2>&1 
check;
done
}


function fullreset_k8s()
{
action_args="fureset vms"
for vm_name in $k8s_nodes;
do
virsh destroy  $vm_name >/dev/null 2>&1 
sleep 1;
virsh undefine $vm_name >/dev/null 2>&1 
rm -rf /vms/data/disks/${vm_name}-*.qcow2 >/dev/null 2>&1 
cp -ra /vms/bak/k8s/${vm_name}.xml /etc/libvirt/qemu/ >/dev/null 2>&1 
cp -ra /vms/bak/k8s/${vm_name}-*.qcow2 /vms/data/disks/ >/dev/null 2>&1 
virsh define /etc/libvirt/qemu/${vm_name}.xml >/dev/null 2>&1 
virsh start ${vm_name} >/dev/null 2>&1 
check;
done
}

function fullreset_exam_k8s()
{
action_args="fureset vms"
for vm_name in $exam_nodes;
do
virsh destroy  $vm_name >/dev/null 2>&1 
sleep 1;
virsh undefine $vm_name >/dev/null 2>&1 
rm -rf /vms/data/disks/${vm_name}-*.qcow2 >/dev/null 2>&1 
cp -ra /vms/bak/exam/${vm_name}.xml /etc/libvirt/qemu/ >/dev/null 2>&1 
cp -ra /vms/bak/exam/${vm_name}-*.qcow2 /vms/data/disks/ >/dev/null 2>&1 
virsh define /etc/libvirt/qemu/${vm_name}.xml >/dev/null 2>&1 
virsh start ${vm_name} >/dev/null 2>&1 
check;
done
}
function fullreset_ceph()
{
action_args="fureset vms"
for vm_name in $ceph_nodes;
do
virsh destroy  $vm_name >/dev/null 2>&1 
sleep 1;
virsh undefine $vm_name >/dev/null 2>&1 
rm -rf /vms/data/disks/${vm_name}-*.qcow2 >/dev/null 2>&1 
cp -ra /vms/bak/ceph/${vm_name}.xml /etc/libvirt/qemu/ >/dev/null 2>&1 
cp -ra /vms/bak/ceph/${vm_name}-*.qcow2 /vms/data/disks/ >/dev/null 2>&1 
virsh define /etc/libvirt/qemu/${vm_name}.xml >/dev/null 2>&1 
virsh start ${vm_name} >/dev/null 2>&1 
check;
done
}


case $1 in
reset-exam)
reset_exam_k8s;;
fullreset-exam)
fullreset_exam_k8s;;
reset-ceph)
reset_ceph;;
reset-k8s)
reset_k8s;;
fullreset-ceph)
fullreset_ceph;;
fullreset-k8s)
fullreset_k8s;;
*)
echo "输入的格式有误，请输入:reset-exam|reset-k8s|reset-ceph|fullreset-k8s|fullreset-ceph";;
esac

